var realObj=(function ($) {
	var donePull,unFinishPull;
	var doneStart = 0, unStart = 0,Limit = 20;
	var donePulling = false,unFinishPulling = false;
	var doneData,unFinishData;
	var nurseid=""
	$.init();
	$.plusReady(function () {
	    baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	
	//已完成
	function getDoneData(){
		if (donePulling) {
			donePull.endPullUpToRefresh()
			return;
		}
		donePulling = true
		var pdict = {'parr':'^^Y','input':$('#search')[0].value,'loginID':nurseid,'start':doneStart,'limit':Limit}
		var paramDict = {'method':'web.INMTeaComm:FindLearnDryRunList','params':JSON.stringify(pdict),'type':'RecQuery'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var dataArr = result.data
			if (!dataArr) {
				donePull.endPullUpToRefresh();
				donePulling = false
				return;
			}
			
			if (doneStart == 0) {
				doneData = dataArr;
			}else{
				doneData = doneData.concat(dataArr)
			}
			updateDoneUI(dataArr)
			if (dataArr.length < Limit) {
				donePull.endPullUpToRefresh(true);
			}else{
				donePull.endPullUpToRefresh();
			}
			doneStart += dataArr.length;
			donePulling = false
		}, function(errorStr) {
		 	donePull.endPullUpToRefresh();
		 	donePulling = false
		});
	}
	function updateDoneUI(dataArr){
		var doneUL = document.getElementById("doneUL");
		if (doneStart == 0) {
			doneUL.innerHTML = ''
		}
		var oldCount = doneUL.querySelectorAll('li').length
		for(var i=0; i<dataArr.length; i++){
			var oneData = dataArr[i]
			var iconStr = 'nur-notPass'
			var line = parseInt(oneData['ReleasePass'])
			var ExamScore = parseInt(oneData['ExamScore'])
			var AnswerDate = oneData['AnswerDate']
			if (AnswerDate != ''&&ExamScore >= line) {
				iconStr = 'nur-pass'
			}
			var html = '<div class="classTitle">' + oneData['ReleaseDesc'] + '</div>'+
				'<div class="classSubInfo">总题数：' + oneData['SubSum'] + ' | 满分：' + oneData['Score'] 
					+ ' | 创建时间：' + oneData['CreatDate'] + ' ' + oneData['CreatTime'] + '</div>'+
				'<div class="scoreImg nurfont '+iconStr+'"></div>'+
				'<div class="scoreArea">成绩：'+oneData['ExamScore']+' <div class="examTime">用时：'+oneData['ExamTime']+'</div></div>'
			
			var newLi = document.createElement('li')
			newLi.setAttribute('class','mui-table-view-cell')
			newLi.innerHTML = html
			doneUL.appendChild(newLi)
		}
	}
	//未完成
	function getUnFinishData(){
		if (unFinishPulling) {
			unFinishPull.endPullUpToRefresh()
			return;
		}
		unFinishPulling = true
		var pdict = {'parr':'^^Y','input':$('#search')[0].value,'loginID':nurseid,'start':unStart,'limit':Limit}
		var paramDict = {'method':'web.INMTeaComm:FindLearnDryRunList','params':JSON.stringify(pdict),'type':'RecQuery'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var dataArr = result.data
			if (!dataArr) {
				unFinishPull.endPullUpToRefresh();
				unFinishPulling = false
				return;
			}
			
			if (unStart == 0) {
				unFinishData = dataArr;
			}else{
				unFinishData = unFinishData.concat(dataArr)
			}
			updateUnFinishUI(dataArr)
			if (dataArr.length < Limit) {
				unFinishPull.endPullUpToRefresh(true);
			}else{
				unFinishPull.endPullUpToRefresh();
			}
			unStart += dataArr.length;
			unFinishPulling = false
		}, function(errorStr) {
		 	unFinishPull.endPullUpToRefresh();
		 	unFinishPulling = false
		});
	}
	// {"rw":"22","AnswerDate":"2022-04-13","AnswerTime":"14:36","CreatDate":"2022-04-13","CreatTime":"14:33",
	// "Creator":"1","Duration":"10","ExamScore":"0","ExamTime":"00:00:08","PaperType":"I","RealPaperType":"I",
	// "ReleaseDesc":"11","ReleasePaper":"","ReleasePass":"20","Score":"10","Status":"Y","ReleaseType":"54||1",
	//"ReleaseTypeDesc":"三基","QuestRange":"S」53||1」1」10」10","StatusDesc":"已完成","SubSum":"1","53__1":"1"}
	function updateUnFinishUI(dataArr){
		var unFinishUL = document.getElementById("unFinishUL");
		if (unStart == 0) {
			unFinishUL.innerHTML = ''
		}
		var oldCount = unFinishUL.querySelectorAll('li').length
		for(var i=0; i<dataArr.length; i++){
			var oneData = dataArr[i]
			var html = '<div class="classTitle">' + oneData['ReleaseDesc'] + '</div>'+
				'<div class="classSubInfo">总题数：' + oneData['SubSum'] + ' | 满分：' + oneData['Score'] 
					+ ' | 创建时间：' + oneData['CreatDate'] + ' ' + oneData['CreatTime'] + '</div>'
			var newLi = document.createElement('li')
			newLi.setAttribute('class','mui-table-view-cell')
			newLi.innerHTML = html
			newLi.id = oldCount + i +'-li'
			unFinishUL.appendChild(newLi)
			newLi.addEventListener('tap',tableViewCellTapped)
		}
	}
	
	function tableViewCellTapped(){
		var index = parseInt(this.id)
		var data = unFinishData[index]
		var webview = plus.webview.create('examRealPaper.html', 'examRealPaper', {
			background: '#fff',
			statusbar: {
				background: '#fff'
			}
		},{fromPage:'examReal',pageType:'',sourceData:data});
		webview.show('fade-in');
	}
	
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		var perInfo = app.getState()
		nurseid = perInfo['loginID']
		mui("body").on("tap", ".searchArea .mui-icon-clear", function() {
			if (document.getElementById('search').value != "") {
				$('#search')[0].value = ""
			}
			refreshMainPage()
		});
		var slider = document.getElementById('slider');
		slider.addEventListener('slide', function(event) {
			var index = event.detail.slideNumber;
			refreshMainPage(index)
			$('#sliderIndicator .mui-indicator[data-index="'+index+'"]')[0].classList.add('mui-active');
		});
		var indicators = document.querySelectorAll('#sliderIndicator .mui-indicator');
		for (var i = 0; i < indicators.length;i++) {
			indicators[i].addEventListener('tap',function(){
				var oldActive = $('#sliderIndicator .mui-indicator.mui-active')[0];
				if (oldActive) {
					if (oldActive == this) {
						return
					}
					oldActive.classList.remove('mui-active')
				}
				var newIndex = parseInt(this.getAttribute('data-index'))
				refreshMainPage(newIndex)
				this.classList.add('mui-active')
				mui('#slider').slider().gotoItem(newIndex,100)
			});
		}
		var search = document.getElementById('search');
		search.addEventListener('input',function(){
			refreshMainPage()
		});
		//已完成
		var doneScroll = document.getElementById('doneScroll');
		donePull = $(doneScroll).pullToRefresh({
			up: {
				callback: function() {
					getDoneData()
				}
			}
		});
		//未完成
		var unFinishScroll = document.getElementById('unFinishScroll');
		unFinishPull = $(unFinishScroll).pullToRefresh({
			up: {
				callback: function() {
					getUnFinishData()
				}
			}
		});
		getDoneData();
		getUnFinishData()
	}
	function refreshMainPage(newIndex){
		if (newIndex == undefined) {
			resetDoneRefresh();
			resetUnFinishRefresh()
			return
		}
		if (newIndex == 0){
			resetDoneRefresh();
		}else{
			resetUnFinishRefresh()
		}
	}
	function resetDoneRefresh(){
		doneStart = 0;
		doneData = []
		if (donePull) {
			donePull.refresh(true)
		}
		getDoneData();
		$('#doneWrapper').scroll().scrollTo(0,0,100);
	}
	function resetUnFinishRefresh(){
		unStart = 0;
		unFinishData = []
		if (unFinishPull) {
			unFinishPull.refresh(true)
		}
		getUnFinishData();
		$('#unFinishWrapper').scroll().scrollTo(0,0,100);
	}
	var refreshPageData = refreshMainPage
	var exportObj = {}
	exportObj.refreshPageData = refreshPageData
	return exportObj
})(mui);

function refreshPageData(){
	realObj.refreshPageData()
}